package cn.webestar.scms.controller;

import cn.webestar.scms.commons.R;
import cn.webestar.scms.security.Authentication;
import cn.webestar.scms.security.SessionContextHolder;
import cn.webestar.scms.user.api.UserApi;
import cn.webestar.scms.user.entity.User;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@Slf4j
@Validated
@RestController
@Tag(name = "用户接口")
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserApi userApi;

    @PostMapping("/findMyPermissions")
    public R<Set<String>> findMyPermissions(){
        Authentication auth = SessionContextHolder.getContext().getAuth();
        Set<String> permissions = auth.getPermissions();
        return R.success(permissions);
    }

    @PostMapping("/findById")
    public R<User> findById(@RequestParam("id") Long id){
        R<User> r = userApi.findById(id);
        return R.success(r.getData());
    }

}
